package com.shipwe.loans

import android.annotation.SuppressLint
import android.content.Context
import androidx.core.content.ContextCompat
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.shipwe.loans.base.BaseApplication
import com.shipwe.loans.http.HttpHelper
import com.shipwe.loans.http.HttpLoader
import com.shipwe.loans.utils.ShipweDateUtil
import com.shipwe.loans.utils.event.AppEventPublisher
import com.shipwe.loans.utils.helper.ShipweAppAnalysisHelper
import com.shipwe.loans.utils.shared.ShipweSharedHelper
import com.shipwe.loans.utils.shared.ShipweSharedKey

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/18 13:35
 * @Annotation：
 */
class MyApps: BaseApplication() {
    //事件订阅
    lateinit var publisher: AppEventPublisher

    companion object {
        // 全局Context
        @SuppressLint("StaticFieldLeak")
        lateinit var context: Context

        @SuppressLint("StaticFieldLeak")
        lateinit var app: MyApps

        //static 代码段可以防止内存泄露
        init {
            //设置全局的Header构建器
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
                layout.setPrimaryColorsId(R.color.transparent, R.color.theme) //全局设置主题颜色
                MaterialHeader(context).setColorSchemeColors(ContextCompat.getColor(context,R.color.theme)) //.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
            //设置全局的Footer构建器
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ -> //指定为经典Footer，默认是 BallPulseFooter
                ClassicsFooter(context).setDrawableSize(20f)
            }
        }
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        context = base
        app = this
        publisher = AppEventPublisher()
    }

    override fun onCreate() {
        super.onCreate()

        //保存APP启动时间
        val startTime = ShipweDateUtil.getFormatTime(System.currentTimeMillis(),"dd/MM/yyyy HH:mm:ss")
        ShipweSharedHelper.putShipweSharedData(ShipweSharedKey.SHIPWE_APP_START_TIME,startTime)

        ShipweSharedHelper.putShipweSharedData(ShipweSharedKey.SHIPWE_SECRET_KEY,"oAgEL6ij5yIJFIQa")
        ShipweSharedHelper.putShipweSharedData(ShipweSharedKey.SHIPWE_SECRET_IV,"tlt0ECkaUg5WVaGP")
        initLibConfigs()
    }

    fun getAppContext(): Context{
        return context
    }

    /**
     * 初始化Lib
     */
    private fun initLibConfigs(){
        HttpHelper.init(HttpLoader())
        //初始化 analysis
        ShipweAppAnalysisHelper.initAnalysisLibAtAppLaunch()
    }

    /**
     * 获取当前手机屏幕的宽
     */
    fun getPhoneWidth(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.widthPixels
    }

    /**
     * 获取当前手机屏幕的高
     */
    fun getPhoneHeight(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.heightPixels
    }
}